package com.jt.www.common.service.impl;

import com.jt.www.common.service.JobQueryListService;
import com.jt.www.dao.mapper.issue.mapper.IssuePolicyLogEntityMapper;
import com.jt.www.dao.mapper.issue.mapper.IssueRecordEntityMapper;
import com.jt.www.model.issue.entity.IssuePolicyLogEntity;
import com.jt.www.model.issue.entity.IssueRecordEntity;
import com.jt.www.util.RedisUtil;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.List;

/**
 * 出单相关-消费记录表
 */
@Service
public class JobQueryListServiceImpl implements JobQueryListService {
    /**
     * IssuePolicyLog 缓存前缀
     */
    private static final String ISSUE_POLICY_LOG_LOCK_KEY = "ISSUE_POLICY_LOG";
    /**
     * IssueRecord 缓存前缀
     */
    private static final String ISSUE_RECORD_LOCK_KEY = "ISSUE_RECORD";
    /**
     * 每个缓存超时时间
     */
    private static final long TIME_OUT = 120L;
    @Autowired
    IssuePolicyLogEntityMapper issuePolicyLogEntityMapper;
    @Autowired
    IssueRecordEntityMapper issueRecordEntityMapper;

    @Override
    public List<IssuePolicyLogEntity> getJobProcessIssuePolicyLogList(Weekend<IssuePolicyLogEntity> weekend) {
        List<IssuePolicyLogEntity> issuePolicyLogEntitys = issuePolicyLogEntityMapper.selectByExample(weekend);

        //准备处理的集合
        List<IssuePolicyLogEntity> processList = Lists.newArrayList();
        for (IssuePolicyLogEntity issuePolicyLogEntity : issuePolicyLogEntitys) {
            String redisKey = RedisUtil.createLockKey(ISSUE_POLICY_LOG_LOCK_KEY, String.valueOf(issuePolicyLogEntity.getId()),
                    String.valueOf(issuePolicyLogEntity.getTimes()));
            //查看当前缓存中是否有该发票 当前次数的处理
            if (RedisUtil.setLock(redisKey, "1", TIME_OUT)) {
                processList.add(issuePolicyLogEntity);
            }
        }
        return processList;
    }

    @Override
    public List<IssueRecordEntity> getJobProcessIssueRecordList(Weekend<IssueRecordEntity> weekend) {

        List<IssueRecordEntity> issueRecordEntities = issueRecordEntityMapper.selectByExample(weekend);

        //准备处理的集合
        List<IssueRecordEntity> processList = Lists.newArrayList();
        for (IssueRecordEntity issueRecordEntity : issueRecordEntities) {
            String redisKey = RedisUtil.createLockKey(ISSUE_RECORD_LOCK_KEY, String.valueOf(issueRecordEntity.getId()));
            if (RedisUtil.setLock(redisKey, "1", TIME_OUT)) {
                processList.add(issueRecordEntity);
            }
        }
        return processList;
    }
}
